#!/bin/bash

# Build script for PostgreSQL MCP Server with layered approach
# Usage: ./build.sh [base|service|all]

set -e

IMAGE_NAME="postgres-mcp-base"
SERVICE_IMAGE_NAME="postgres-mcp-server"
VERSION="latest"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Build base image
build_base() {
    print_status "Building base image: ${IMAGE_NAME}:${VERSION}..."
    docker build \
        -f Dockerfile.base \
        -t ${IMAGE_NAME}:${VERSION} \
        -t ${IMAGE_NAME}:$(date +%Y%m%d) \
        .

    if [ $? -eq 0 ]; then
        print_status "Base image built successfully!"
        print_status "Images created:"
        echo "  - ${IMAGE_NAME}:${VERSION}"
        echo "  - ${IMAGE_NAME}:$(date +%Y%m%d)"
    else
        print_error "Failed to build base image!"
        exit 1
    fi
}

# Build service image
build_service() {
    # Check if base image exists
    if ! docker image inspect ${IMAGE_NAME}:${VERSION} &> /dev/null; then
        print_warning "Base image ${IMAGE_NAME}:${VERSION} not found. Building base image first..."
        build_base
    fi

    print_status "Building service image: ${SERVICE_IMAGE_NAME}:${VERSION}..."
    docker build \
        -f Dockerfile.service \
        -t ${SERVICE_IMAGE_NAME}:${VERSION} \
        -t ${SERVICE_IMAGE_NAME}:$(date +%Y%m%d) \
        .

    if [ $? -eq 0 ]; then
        print_status "Service image built successfully!"
        print_status "Images created:"
        echo "  - ${SERVICE_IMAGE_NAME}:${VERSION}"
        echo "  - ${SERVICE_IMAGE_NAME}:$(date +%Y%m%d)"
    else
        print_error "Failed to build service image!"
        exit 1
    fi
}

# Build both images
build_all() {
    print_status "Building all images..."
    build_base
    build_service
}

# Show usage
show_usage() {
    echo "Usage: $0 [base|service|all]"
    echo ""
    echo "Commands:"
    echo "  base    - Build only the base image with system dependencies"
    echo "  service - Build only the service image with application code"
    echo "  all     - Build both base and service images (default)"
    echo ""
    echo "Examples:"
    echo "  $0 base     # Build base image only"
    echo "  $0 service  # Build service image only (fast)"
    echo "  $0 all      # Build both images"
}

# Parse command line arguments
case "${1:-all}" in
    "base")
        build_base
        ;;
    "service")
        build_service
        ;;
    "all")
        build_all
        ;;
    "help"|"-h"|"--help")
        show_usage
        ;;
    *)
        print_error "Unknown command: $1"
        show_usage
        exit 1
        ;;
esac

print_status "Build process completed!"

# Show image sizes
print_status "Image sizes:"
docker images | grep -E "${IMAGE_NAME}|${SERVICE_IMAGE_NAME}" | while read line; do
    echo "  $line"
done